<template>
  <div>
    <el-card class="box-card">
      <PageTools :totalNum="total">
        <!-- 新增按钮 -->
          <template #buttonPart>
            <el-row>
              <el-col :span="24">
                <el-button type="success" icon="el-icon-edit" size="medium" @click="$router.push('/questionbank/TestEntry')">新增文章</el-button>
              </el-col>
          </el-row>
          </template>
        <!-- 表单 -->
        <template #formPart>
          <searchQuest @onSearch="onSearch"></searchQuest>
        </template>
      </PageTools>

      <!-- 表格：展示数据 -->
      <div class="table-content">
        <el-table
          v-loading="loading"
          :data="listData"
          style="width: 100%">
          <el-table-column
            align="center"
            prop="number"
            label="试题编号"
            width="120">
          </el-table-column>

          <el-table-column
            align="center"
            prop="subject"
            label="学科"
            width="120">
          </el-table-column>

          <el-table-column
            align="center"
            prop="catalog"
            label="目录"
            width="120">
          </el-table-column>

          <el-table-column
            align="center"
            label="题型"
            width="100">
            <template #default="{row}">
              <span v-if="row.questionType === '1'">单选</span>
              <span v-else-if="row.questionType === '2'">多选</span>
              <span v-else>简答</span>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            label="题干"
            width="200">
            <template #default="{row}">
              <div v-html="row.question"></div>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            label="录入时间"
            width="100">
            <template #default="{row}">
              <span>{{ row.createTime | timeTrans }}</span>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            label="难度"
            width="120">
            <template #default="{row}">
              <span v-if="row.difficulty === '1'">简单</span>
              <span v-else-if="row.difficulty === '2'">一般</span>
              <span v-else>困难</span>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            prop="creator"
            label="录入人"
            width="120">
          </el-table-column>

          <el-table-column align="center" label="操作">
            <template #default="{row}">
              <el-tooltip content="预览" placement="bottom" effect="light">
                <el-button type="primary" plain circle icon="el-icon-view" @click="previewQuestion(row.id)"></el-button>
              </el-tooltip>

              <el-tooltip content="修改" placement="bottom" effect="light">
                <el-button type="success" plain circle icon="el-icon-edit" @click="updateQuest(row.id)"></el-button>
              </el-tooltip>

              <el-tooltip content="删除" placement="bottom" effect="light">
                <el-button type="danger" plain circle icon="el-icon-delete" @click="delItem(row.id)"></el-button>
              </el-tooltip>

              <el-tooltip content="加入精选" placement="bottom" effect="light">
                <el-button type="warning" plain circle icon="el-icon-check" @click="addSelected(row.id)"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <div class="page-divided">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="params.pagesize"
          layout="sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <PreviewDialog
      :visible.sync="visible"
      :passId="passId"
    ></PreviewDialog>
  </div>
</template>

<script>
import { getBaseList, delBaseList, addToSelectedBank } from '@/api/querstionbank'
import PageTools from '../components/pageTools.vue'
import searchQuest from '../components/searchQuest.vue'
import PreviewDialog from '../components/previewDialog.vue'

export default {
  name: 'BaseQuestionBank',
  components: {
    PageTools,
    searchQuest,
    PreviewDialog
  },
  data() {
    return {
      params: {
        page: 1,
        pagesize: 10
      },
      currentPage: 1,
      listData: [],
      total: 0,
      passId: null,
      visible: false,
      loading: false,
      passObject: null
    }
  },

  methods: {
    // 获取全部文章列表
    async getList(data) {
      this.loading = true
      const res = await getBaseList(data)
      this.total = res.counts
      this.listData = res.items
      this.loading = false
    },
    // 处理分页
    handleSizeChange(val) {
      this.params.pagesize = val
      if(!this.passObject){
         this.getList(this.params)
      }else {
        this.onSearch()
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.params.page = val
      if(!this.passObject){
         this.getList(this.params)
      }else {
        this.onSearch()
      }
    },
    // 删除基础题目
    delItem(id) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await delBaseList(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList(this.params)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 加入精选题库
    addSelected(id) {
      this.$confirm('是否将题目加入精选题库?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await addToSelectedBank(id, 1)
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          this.$router.push('/questionbank/selectedqb')
          this.getList(this.params)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    },
    onSearch(obj){
      this.passObject = obj
      if(obj){
        this.getList({...this.params, ...obj})
      }else {
        this.getList(this.params)
      }
    },
    updateQuest(id){
      this.$router.push({
        path: '/questionbank/TestEntry' ,
        query: { id: id }
      })
    },
    previewQuestion(id) {
      this.visible = true
      this.passId = id
    }
  },

  created() {
    this.getList(this.params)
    this.passObject = null
  }
}
</script>

<style lang="scss" scoped>
.page-divided{
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
.table-content{
  margin-top: 20px;
}

</style>
